<template>
  <div class="latest-news">
    <h2 class="section-title">最新动态</h2>
    <div class="news-container">
      <!-- 动态生成的新闻卡片 -->
      <div
          v-for="(news, index) in newsList"
          :key="news.id"
          class="news-card"
          :class="`card-${index % 4}`"
          @mouseenter="hoverCard(index)"
          @mouseleave="resetCard"
          @click="goToDetail(news.id)"
      >
        <div class="news-date">{{ formatDate(news.publishTime) }}</div>
        <h3 class="news-title">{{ news.title }}</h3>
        <p class="news-content">
          {{ news.summary }}
        </p>
        <div
            class="news-background"
            :style="{ backgroundImage: `url(${news.coverImage})` }"
        ></div>
        <div class="news-overlay"></div>
        <button class="read-more">阅读更多</button>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <el-icon class="loading-icon"><Loading /></el-icon>
      加载中...
    </div>

    <!-- 分页控件 -->
    <div class="pagination" v-if="total > pageSize">
      <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          v-model:current-page="currentPage"
          @current-change="fetchNews"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Loading } from '@element-plus/icons-vue'
import { selectLawyer } from '@/api/test.js' // 根据实际路径调整
const router = useRouter()

// 数据状态
const newsList = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = 6
const activeCard = ref(null)

// 响应式数据
const lawyerList = ref([])
const testLoading = ref(false)
const error = ref(null)

// 获取律师列表函数
async function fetchLawyers() {
  testLoading.value = true
  error.value = null

  try {
    const response = await selectLawyer()
    lawyerList.value = response.data // 根据实际返回数据结构调整
  } catch (err) {
    error.value = err.message || '获取律师列表失败'
    console.error('获取律师列表错误:', err)
  } finally {
    testLoading.value = false
  }
}

// 卡片悬停效果
const hoverCard = (index) => {
  activeCard.value = index
}

const resetCard = () => {
  activeCard.value = null
}

// 日期格式化
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return `${date.getFullYear()}.${(date.getMonth() + 1).toString().padStart(2, '0')}.${date.getDate().toString().padStart(2, '0')}`
}

// 跳转详情页
const goToDetail = (id) => {
  router.push(`/news/${id}`)
}

// 模拟API请求
const fetchNews = async (page = 1) => {
  try {
    loading.value = true

    // 实际项目中替换为真实的API调用
    const mockApi = () => {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve({
            data: [
              {
                id: 1,
                title: '鞠婧祎是沪上阿姨首位代言人',
                summary: '沪上阿姨于2025年5月10日正式宣布鞠婧祎为首位女性品牌代言人，合作主题为“东方摩登茶生活”。官宣后迅速引发社交媒体现象级传播，相关话题阅读量超2亿，品牌小程序订单量当日同比上涨180%',
                publishTime: '2025-04-17T10:00:00',
                coverImage: 'https://via.placeholder.com/800x600?text=AI+News'
              },
              {
                id: 2,
                title: '鞠婧祎是Mac口红代言人',
                summary: '鞠婧祎主推新品唇釉（如“蜜渍桃桃”色号）及无瑕底妆系列，唇釉管身烫金印有其英文名缩写，强化个人IP与品牌关联',
                publishTime: '2025-05-19T14:30:00',
                coverImage: 'https://via.placeholder.com/800x600?text=WeChat'
              },
              // 更多模拟数据...
            ],
            total: 12
          })
        }, 500)
      })
    }

    const response = await mockApi()
    newsList.value = response.data
    total.value = response.total
    currentPage.value = page

  } catch (error) {
    console.error('获取新闻数据失败:', error)
  } finally {
    loading.value = false
  }
}

// 初始化加载数据
onMounted(() => {
  fetchNews()
  fetchLawyers()
})
</script>

<style scoped>
.latest-news {
  padding: 80px 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.section-title {
  font-size: 2rem;
  margin-bottom: 60px;
  text-align: center;
  color: #333;
  position: relative;
  font-weight: 500;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 3px;
  background: linear-gradient(90deg, #00a1ff, #8f00ff);
}

.news-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}

.news-card {
  position: relative;
  height: 320px;
  border-radius: 16px;
  padding: 30px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
  background: white;
  cursor: pointer;
}

.news-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.12);
}

.news-date {
  font-size: 0.9rem;
  margin-bottom: 15px;
  color: #666;
  position: relative;
  z-index: 3;
}

.news-title {
  font-size: 1.4rem;
  margin-bottom: 20px;
  line-height: 1.3;
  position: relative;
  z-index: 3;
  color: #333;
  transition: all 0.3s ease;
}

.news-content {
  font-size: 1rem;
  line-height: 1.6;
  color: #666;
  position: relative;
  z-index: 3;
  margin-bottom: 30px;
  transition: all 0.3s ease;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.news-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  z-index: 1;
  opacity: 0.1;
  transition: all 0.5s ease;
}

.news-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, rgba(255,255,255,0.9) 0%, rgba(255,255,255,0.7) 100%);
  z-index: 2;
  transition: all 0.4s ease;
}

.read-more {
  position: relative;
  z-index: 3;
  background: transparent;
  border: 1px solid #00a1ff;
  color: #00a1ff;
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.3s ease;
  opacity: 0;
  transform: translateY(10px);
}

.news-card:hover .read-more {
  opacity: 1;
  transform: translateY(0);
}

.read-more:hover {
  background: #00a1ff;
  color: white;
}

/* 卡片悬停效果 */
.news-card:hover .news-overlay {
  opacity: 0.7;
}

.news-card:hover .news-background {
  opacity: 0.2;
  transform: scale(1.05);
}

.news-card:hover .news-title {
  transform: translateY(-5px);
}

.news-card:hover .news-content {
  transform: translateY(-3px);
}

/* 不同卡片样式 */
.card-0:hover .read-more {
  border-color: #00a1ff;
  color: #00a1ff;
}
.card-0:hover .read-more:hover {
  background: #00a1ff;
  color: white;
}

.card-1:hover .read-more {
  border-color: #8f00ff;
  color: #8f00ff;
}
.card-1:hover .read-more:hover {
  background: #8f00ff;
  color: white;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #666;
}

.loading-icon {
  font-size: 2rem;
  margin-bottom: 10px;
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 分页样式 */
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .news-container {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .news-card {
    height: 280px;
    padding: 25px;
  }

  .section-title {
    font-size: 1.8rem;
    margin-bottom: 40px;
  }
}
</style>